home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_02_11 / 2n11022a < prev    next >
Text File  |  1991-09-11  |  3KB  |  92 lines

  1. /*
  2.  *  LISTING 2
  3.  *
  4.  *  bcache.h
  5.  *
  6.  *  Header for block cache routines using
  7.  *  M/LFRU algorithm.
  8.  *
  9.  *  Source code Copyright (c) 1991 T.W. Nelson. May be
  10.  *  used only for non-commercial purposes with
  11.  *  appropriate acknowledgement of copyright.
  12.  */
  13.  
  14. #ifndef __BCACHE_H
  15. #define __BCACHE_H
  16.  
  17. #ifndef __STDIO_H
  18. #include <stdio.h>
  19. #endif
  20.  
  21. typedef unsigned long   ulong;
  22. typedef struct cchdr far chdr_t;  //block hdr object
  23. typedef void far bufp_t;          //buffer contents
  24. typedef void far idnt_t;          //cache id type
  25.  
  26. /* Block buffer header. This immediately precedes each
  27.  * block buffer in memory. Total memory required for
  28.  * each block is therefore:
  29.  *       (sizeof(struct cchdr) + bsiz) bytes
  30.  */
  31.  
  32. struct  cchdr {
  33.     chdr_t *next;   //next block header
  34.     chdr_t *prev;   //prev block header
  35.     size_t stat;    //block status
  36.     idnt_t *idnt;   //cache id for proc()
  37.     ulong  bnum;    //block number
  38.     ulong  acc;     //access count
  39.     char bufp;      //&bufp == buffer start
  40.     };
  41.  
  42. /* Cache descriptor type. Storage for one of these is
  43.  * allocated by the application for each cache opened.
  44.  */
  45. typedef struct {
  46.     idnt_t *idnt;  //cache id (matches cchdr.idnt)
  47.     int bsiz;      //block length in bytes
  48.     int bmax;      //max number blocks in cache
  49.     ulong  hits;   //bc_search() finds
  50.     ulong  miss;   //bc_search() misses
  51.     ulong  adds;   //number calls to bc_addnew()
  52.                    //block processing function ...
  53.     void (*proc)(idnt_t *, ulong, bufp_t *);
  54.     chdr_t *mfru;  //mfru (newest) block
  55.     chdr_t *lfru;  //lfru (oldest) block
  56.     chdr_t *head;  //-> allocated cache memory
  57.     size_t user1;  //user-defined data areas
  58.     size_t user2;
  59.     } BCACHE;      //BCACHE object
  60.  
  61. #define MARK    1      //mark blocks for processing
  62. #define RELEASE 0      //release (unmark) for reuse
  63. #define ALLOCATE    0  //allocate memory
  64. #define DEALLOCATE  1  //deallocate memory
  65. #define GROUND  ((chdr_t *) -1)     //end of chain
  66. #define BHDR_SIZE   sizeof(struct cchdr)
  67. #define BLOCK_PTR(type,hp) ((type *) &(hp->bufp))
  68.  
  69. //Function return values ( >= 0 ) ....
  70. #define BC_NOTFOUND     1   //block not found
  71. #define BC_NOERROR      0   //no error
  72.  
  73. //Function error values ( < 0 ) ....
  74. #define BC_NOBLOCKS     -1  //need >= 2 blocks
  75. #define BC_NOMEMORY     -2  //insufficient memory
  76.  
  77. //prototypes ......
  78. int bc_open( BCACHE *c, int bmax,
  79.              int bsize, void (*proc)(), idnt_t *idnt);
  80. int bc_alloc( BCACHE *c, int flag );
  81. int bc_search( BCACHE *c, ulong bnum, bufp_t **bufptr);
  82. int bc_insert( BCACHE *c, chdr_t *p );
  83. int bc_addnew( BCACHE *c, ulong bnum, bufp_t **bufptr);
  84. int bc_mark( BCACHE *c, ulong bnum, size_t flag );
  85. int bc_flush( BCACHE *c );
  86. int bc_free( BCACHE *c );
  87. int bc_setalloc( int (*user_alloc)() );
  88.  
  89. #endif   // __BCACHE_H
  90.  
  91. /* ---- End of File -------------------------------- */
  92.